/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.edu.upc.sad.dao;

import java.awt.Color;
import java.util.List;
import java.util.ArrayList;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import pe.edu.upc.sad.model.Usuario;
import pe.edu.upc.sad.util.BError;
import pe.edu.upc.sad.util.Conexion;
import pe.edu.upc.sad.util.ToolSql;


public class UsuarioDaoImpl implements UsuarioDao
{
    Connection conn = null;
    private PreparedStatement pstm = null;
    private Statement  st   = null;
    private ResultSet  rs   = null;
    private CallableStatement stmt =null;
    private StringBuffer query = null;

    private BError error;

    public UsuarioDaoImpl()
    {
    
    }

   public Usuario get(String dni, String clave) throws Exception
   {
         try
         {
              Inicio();

            Usuario user = null;
            query.append("{ call USP_SELECT_USUARIO( ?, ? ) }");
            stmt = conn.prepareCall(query.toString());

            stmt.setString(1,dni);
            stmt.setString(2,clave);
            stmt.execute();
            rs = stmt.getResultSet();
            while (rs.next())
            {
              user = new Usuario();
              user.setNombres(rs.getString("Nombres"));
              user.setDni(rs.getString("Dni"));
              user.setDireccion(rs.getString("Direccion"));
              user.setTelefono(rs.getString("Telefono"));
              user.setEmail(rs.getString("Email"));
              user.setApellidos(rs.getString("Apellidos"));
            }
           return user;

        }
        catch (Exception e)
        {
            error.setCodError(100);
            error.setMsgError("Error :" + e.getMessage());
            return null;
        }finally{
            ToolSql.close(rs, st, stmt, conn);
        }
       
   }

   public String Color()
   {
        Random random = new Random();
        Color randomColor = Color.getHSBColor( random.nextFloat(), 1.0F, 1.0F );
        String rgb = Integer.toHexString(randomColor.getRGB());
        rgb = rgb.substring(2, rgb.length());
        rgb = rgb.toString();
        return rgb;

    }

   public int insert(Usuario user) throws SQLException
   {
   int resultado = 0;
   try
    {
        Inicio();

      query.append("{ call USP_INSERT_USUARIO( ?, ?, ?, ?, ?, ?, ?, ?  ) }");
      
      stmt = conn.prepareCall(query.toString());

      stmt.setString(1,user.getDni());
      stmt.setString(2,user.getNombres());
      stmt.setString(3,user.getApellidos());
      stmt.setString(4,user.getDireccion());
      stmt.setString(5,user.getTelefono());
      stmt.setString(6,user.getClave());
      stmt.setString(7,user.getEmail());
      stmt.setString(8,Color());


      stmt.execute();

      resultado = 1;//stmt.getInt(32);
      return resultado;
    }
    catch(Exception e)
    {
        error.setCodError(100);
        error.setMsgError(e.getMessage());
         return 0;
    }
    finally
    {
        ToolSql.close(rs, st, stmt, conn);
    }
    
  }

   public List<Usuario> getListUser(Connection conn) throws Exception
   {

    List lstUser = new ArrayList<Usuario>();

    try
    {
         Inicio();

      stmt = conn.prepareCall( "call USP_LISTAR_USUARIO" );

      rs = stmt.executeQuery();
        
        while (rs.next())
        {

          Usuario user = new Usuario();

          user.setNombres(rs.getString("Nombre"));
          user.setDni(rs.getString("Dni"));
          user.setDireccion(rs.getString("Direccion"));
          user.setTelefono(rs.getString("Telefono"));
          user.setEmail(rs.getString("Email"));
          user.setApellidos(rs.getString("Apellidos"));
          lstUser.add(user);

        }
    }catch(Exception e)
    {
        error.setCodError(100);
        error.setMsgError("Error :" + e.getMessage());
    }
    finally
    {
      try
      {
       // ToolSql.close(rs,null,stmt,null );
         ToolSql.close(rs, st, stmt, conn);
      }
      catch (SQLException se)
      {
       throw se;
       }
    }
    return  lstUser;
  }

   public void Inicio()
   {
       error = new BError();
       query = new StringBuffer("");
       conn =   Conexion.getConnection();
    }
}
